﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace SPAdvancedSearch.Inputs
{
    /// <summary>
    /// Extends the <see cref="DropDownList"/> to support the <see cref="ISearchPropertyInputControl"/> interface.
    /// </summary>
    public class DropDownListEx : System.Web.UI.WebControls.DropDownList, ISearchPropertyInputControl
    {

        #region Fields

        private string _propertyValue;

        #endregion

        #region ISearchPropertyInputControl Members

        /// <summary>
        /// Gets or sets the value for the drop-down list.
        /// </summary>
        public string PropertyValue
        {
            get
            {
                return this.SelectedValue;
            }
            set
            {
                _propertyValue = value;
            }
        }

        #endregion

        /// <summary>
        /// Overridden to set the selected item based on the property value.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (!string.IsNullOrEmpty(_propertyValue))
            {
                ListItem item = this.Items.FindByValue(_propertyValue);
                if (item != null)
                    item.Selected = true;
            }
        }
    }
}
